/**
 * Created by dongchen on 16/4/18.
 */
var mongoose = require('mongoose');
var config = require('config');

mongoose.connect(config.mongo_url);
console.log('--------------------- mongo connected --------------------');

var Schema = mongoose.Schema,
    ObjectId = Schema.ObjectId;

//status 0 = 关注, status 1 = 已取关
var User = new Schema({
    id      : ObjectId,
    mobile  : String,
    open_id : {type : String, index : true},
    card_id : String,
    create_time : {type : Date, default: Date.now},
    update_time : {type : Date, default: Date.now},
    status  : Number
});

var Card = new Schema({
    id          : ObjectId,
    iccid       : String,
    mobile      : {type : String, unique : true},
    card_id     : {type : String, unique : true}, //YYMMDD+seq(5位) 系统内建卡号,唯一标识
    card_pwd    : String,
    open_time   : {type : Date, default: Date.now},
    create_time : {type : Date, default: Date.now},
    update_time : {type : Date, default: Date.now}
});

var Order = new Schema({
    id          : ObjectId,
    order_id    : {type : String, unique : true},   //内部订单号, 格式:YYMMDDHHmmss-openId前4位-seq4位,共20位
    outer_task_id       : String,                   //外部充值任务id
    outer_task_report   : Schema.Types.Mixed,       //外部充值任务反馈状态报告
    mobile      : String,                           //订单充值号码
    amount      : Number,                           //单位(分)
    package     : String,                           //充值流量包大小
    payer_id    : ObjectId,                         //充值用户id
    status      : Number,                           //订单状态: 0=未付款, 1=订单验证失败, 2=已付款, 3=付款失败, 4=充值完成
    type        : {type : Number, default : 1},     //订单类型: 1=流量卡订单, 2=通用流量订单
    create_time : Date,
    update_time : {type : Date, default: Date.now},
    pay_log     : Schema.Types.Mixed
});

var License = new Schema({
    access_token : Schema.Types.Mixed,
    jsapi_ticket : Schema.Types.Mixed
});


var userModel = mongoose.model('user', User);
var cardModel = mongoose.model('card', Card);
var OrderModel = mongoose.model('order', Order);
var LicenseModel = mongoose.model('license', License);

module.exports = {
    User : userModel,
    Card : cardModel,
    Order : OrderModel,
    License : LicenseModel
};


